const fileUtil = require('../../common/utils/fileUtil')
const pluginFeatureDAO = require('../../core/rdsService/pluginFeatureDAO');
const envUtil = require('../../common/utils/envUtil')
const uuidUtil = require('../../common/utils/uuidUtil')


exports.updatePluginFeatureByPluginModel = function (pluginInfoModel) {
    featureParse(pluginInfoModel)
}


function featureParse(pluginInfoModel) {
    let thePluginInfoModel = pluginInfoModel
    const features = thePluginInfoModel.pluginInfo.features
    thePluginInfoModel.pluginInfo.logo = thePluginInfoModel.pluginInfo.logo ? fileUtil.pluginResPathWarper(thePluginInfoModel.pluginId, thePluginInfoModel.pluginInfo.logo, true) : null
    const allCmds = []
    for (let feature of features) {
        const platform = feature.platform
        const featureInfo = {
            code: feature.code,
            desc: feature.desc,
            name: feature.name,
            icon: feature.icon ? fileUtil.pluginResPathWarper(thePluginInfoModel.pluginId, feature.icon, true) : null,
            platform: feature.platform,
            mode: feature.mode,
        }
        // 校验当前插件是否适配当前系统
        if (!envUtil.checkPlatform(platform)) {
            continue
        }
        const cmds = feature.cmds
        for (let cmd of cmds) {
            allCmds.push({
                cmdInfo: cmd,
                featureInfo: featureInfo,
                pluginInfo: thePluginInfoModel
            })
        }
    }

    pluginFeatureDAO.getPluginFeatureByCondition(thePluginInfoModel.pluginId, null, "plugin", null, null, null, null, null, (cmds) => {
        for (let item of allCmds) {
            featurePersist(item, cmds)
        }

        for (let item of cmds) {
            featureDestory(item, allCmds)
        }
    })

}


function featureDestory(cmd, allCmds) {
    let keys = []
    for (let item of allCmds) {
        const decisionMethod = item.cmdInfo.decisionMethod
        const decisionDataType = item.cmdInfo.decisionDataType
        const decisionDataSource = item.cmdInfo.decisionDataSource
        const decisionData = item.cmdInfo.decisionData
        const featureCode = item.featureInfo.code

        keys.push(decisionMethod + decisionDataType + decisionDataSource + decisionData + featureCode)
    }



    const theDecisionMethod = cmd.cmdInfo.cmdInfo.decisionMethod
    const theDecisionDataType = cmd.cmdInfo.cmdInfo.decisionDataType
    const theDecisionDataSource = cmd.cmdInfo.cmdInfo.decisionDataSource
    const theDecisionData = cmd.cmdInfo.cmdInfo.decisionData
    const theFeatureCode = cmd.cmdInfo.featureInfo.code


    if (!keys.indexOf(theDecisionMethod + theDecisionDataType + theDecisionDataSource + theDecisionData + theFeatureCode)) {
        pluginFeatureDAO.delPluginFeatureByCmdId(cmd.cmdId)
    }

}



function featurePersist(cmd, cmds) {
    const decisionMethod = cmd.cmdInfo.decisionMethod
    const decisionDataType = cmd.cmdInfo.decisionDataType
    const decisionDataSource = cmd.cmdInfo.decisionDataSource
    const decisionData = cmd.cmdInfo.decisionData
    const pluginId = cmd.pluginInfo.pluginId
    const featureCode = cmd.featureInfo.code


    let extInfo = null


    for (let cmdItem of cmds) {
        const theDecisionMethod = cmdItem.cmdInfo.cmdInfo.decisionMethod
        const theDecisionDataType = cmdItem.cmdInfo.cmdInfo.decisionDataType
        const theDecisionDataSource = cmdItem.cmdInfo.cmdInfo.decisionDataSource
        const theDecisionData = cmdItem.cmdInfo.cmdInfo.decisionData
        const theFeatureCode = cmdItem.cmdInfo.featureInfo.code
        const theExtInfo = cmdItem.extInfo




        if (decisionMethod == theDecisionMethod &&
            decisionDataType == theDecisionDataType &&
            decisionDataSource == theDecisionDataSource &&
            decisionData == theDecisionData &&
            featureCode == theFeatureCode
        ) {
            // 如果已存在相同的cmd，则更新，否则插入
            if (JSON.stringify(cmd) != JSON.stringify(cmdItem.cmdInfo)) {
                cmdItem.cmdInfo = cmd
                pluginFeatureDAO.update(cmdItem)
            }
            return
        }
    }
    const cmdInfo = cmd
    const theFeature = pluginFeatureDAO.composeModel(pluginId, uuidUtil.uuid(), decisionMethod, decisionDataType, decisionDataSource, decisionData, "plugin", cmdInfo, extInfo)
    pluginFeatureDAO.insert(theFeature)

}